package com.itheima.mall.api.sms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.exception.BusinessException;
import com.itheima.mall.mapper.sms.HomeNewProductMapper;
import com.itheima.mall.pojo.sms.HomeNewProduct;
import com.itheima.mall.pojo.sms.SmsHomeBrand;
import com.itheima.mall.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@DubboService
public class HomeNewProductServiceApiImpl implements HomeNewProductServiceApi{
    @Autowired
    private HomeNewProductMapper homeNewProductMapper;
    @Override
    public void newProductCreate(HomeNewProduct homeNewProduct) {
        homeNewProductMapper.insert(homeNewProduct);
    }

    @Override
    public Boolean newProductDelete(Long[] ids) {
        Boolean flag = true;
        List<HomeNewProduct> homeNewProductList = homeNewProductMapper.selectBatchIds(Arrays.asList(ids));
        for (HomeNewProduct homeNewProduct : homeNewProductList) {
            if(homeNewProduct.getRecommendStatus() == 1){
                flag = false;
            }else {
                homeNewProductMapper.deleteById(homeNewProduct.getId());
            }
        }
        return flag;
    }

    @Override
    public PageResult<HomeNewProduct> newProductList(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        Page pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<HomeNewProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(recommendStatus!=null,HomeNewProduct::getRecommendStatus,recommendStatus);
        queryWrapper.like(StrUtil.isNotEmpty(productName),HomeNewProduct::getProductName,productName);
        queryWrapper.orderByDesc(HomeNewProduct::getSort);
        Page page = homeNewProductMapper.selectPage(pageInfo, queryWrapper);
        PageResult pageResult = new PageResult<>();
        pageResult.setTotal(Convert.toInt(page.getTotal()));
        pageResult.setPageSize(Convert.toInt(page.getSize()));
        pageResult.setPageNum(Convert.toInt(page.getCurrent()));
        pageResult.setTotalPage(Convert.toInt(page.getPages()));
        pageResult.setList(page.getRecords());
        return pageResult;
    }

    @Override
    public List<HomeNewProduct> findNewProductByIds(Long[] ids) {
        List<HomeNewProduct> homeNewProductList = homeNewProductMapper.selectBatchIds(Arrays.asList(ids));
        return homeNewProductList;
    }

    @Override
    public void update(HomeNewProduct homeNewProduct) {
        homeNewProductMapper.updateById(homeNewProduct);
    }

    @Override
    public HomeNewProduct findNewProductById(Long id) {
        return homeNewProductMapper.selectById(id);
    }


}
